package com.crm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.crm.common.ResVo;
import com.crm.entity.Comments;
import com.crm.service.CommentsService;
import com.spring4all.swagger.EnableSwagger2Doc;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@EnableSwagger2Doc
@RequestMapping("/comments")
@Api(tags = "评论管理")
public class CommentsController {

	@Autowired
	private CommentsService commentsService;

	@GetMapping("/findConmments/{topicId}/{pageIndex}")
	@ApiOperation("根据主题id查询评论,带分页,默认每页十条数据")
	public ResVo<Object> findConmmentsByTopicId(@PathVariable @ApiParam("评论的主题id") String topicId,
			@PathVariable @ApiParam("分页的页数,默认每页十条数据") Integer pageIndex) {
		Page<Comments> message = commentsService.findConmmentsByTopicId(topicId, pageIndex);
		return ResVo.success(message);
	}

	@GetMapping("/getConmmentsByPage/{pageIndex}")
	@ApiOperation("根据主题id查询评论,带分页,默认每页十条数据")
	public ResVo<Object> getConmmentsByPage(@PathVariable @ApiParam("分页的页数,默认每页十条数据") Integer pageIndex,
			@RequestParam(value = "topicId", required = false) @ApiParam("话题id") String topicId) {
		Page<Comments> message = commentsService.getConmmentsByPage(pageIndex,topicId);
		return ResVo.success(message);
	}

	@GetMapping("/getConmments/{topicId}")
	@ApiOperation("查询所有评论")
	public ResVo<Object> getConmments(@PathVariable @ApiParam("评论的主题id") String topicId) {
		List<Comments> message = commentsService.getConmments(topicId);
		return ResVo.success(message);
	}

	@PostMapping("/addComments")
	@ApiOperation("发表评论")
	public ResVo<Object> addComments(@RequestBody Comments comments) {
		commentsService.addComments(comments);
		return ResVo.defaultSuccess();
	}

}
